package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Utils.BaseDao;
import bean.Cuisine;
import bean.Hotel;

public class HDaoimpl implements HDao {

	@Override
	public List<Hotel> list(String fname,String cid) {
		List<Hotel>list = new ArrayList<Hotel>();
		Connection conn = BaseDao.conn();
		String sql="select hotel.*,cuisine.cname from hotel,cuisine where cuisine.cid=hotel.cid";
		
		if (fname!=null&&fname!="") {
			sql+=" and name like'%"+fname+"%'";
		}
		if (cid!=null&&cid!="") {
			sql+=" and cuisine.cid ="+cid;
		}
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				String sq = rs.getString(5);
				if (sq.length()>10) {
					sq = sq.substring(0,10)+"....";
				}
				Hotel h = new Hotel(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), sq, rs.getString(6), rs.getInt(7), rs.getString(8));
				list.add(h);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int add(Hotel h) {
		Connection conn = BaseDao.conn();
		String sql = "insert into hotel VALUES(null,'"+h.getCid()+"','"+h.getName()+"','"+h.getAddress()+"','"+h.getDescription()+"','"+h.getOpening_time()+"',-1)";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int del(String id) {
		Connection conn = BaseDao.conn();
		try {
			PreparedStatement ps = conn.prepareStatement("delete from hotel where id="+id);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public Hotel to(String id) {
		Connection conn = BaseDao.conn();
		try {
			PreparedStatement ps = conn.prepareStatement("select hotel.*,cuisine.cname from hotel,cuisine where cuisine.cid=hotel.cid and id="+id);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				String sq = rs.getString(5);
				if (sq.length()>10) {
					sq = sq.substring(0,10)+"....";
				}
				Hotel h = new Hotel(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), sq, rs.getString(6), rs.getInt(7), rs.getString(8));
				return h;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public int up(Hotel h) {
		Connection conn = BaseDao.conn();
		System.out.println(h);
		
		String sql="update hotel set name='"+h.getName()+"',address='"+h.getAddress()+"',description='"+h.getDescription()+"',opening_time='"+h.getOpening_time()+"',cid='"+h.getCid()+"',state='"+h.getState()+"' where id="+h.getId();
		System.out.println(sql);
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int state(String id, String state) {
		Connection conn = BaseDao.conn();
		try {
			PreparedStatement ps = conn.prepareStatement("update hotel set state='"+state+"' where id="+id);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<Cuisine> cu() {
		List<Cuisine>cu = new ArrayList<Cuisine>();
		Connection conn = BaseDao.conn();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from cuisine");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Cuisine c = new Cuisine(rs.getInt(1), rs.getString(2), rs.getString(3));
				cu.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cu;
	}
	
}
